package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.node.TreeBack;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.ShiroUser;
import cn.stylefeng.guns.modular.system.entity.DealerLevel;
import cn.stylefeng.guns.modular.system.entity.InventoryOrder;
import cn.stylefeng.guns.modular.system.entity.PutInWarehouse;
import cn.stylefeng.guns.modular.system.entity.ShiWarehouse;
import cn.stylefeng.guns.modular.system.mapper.PutInWarehouseMapper;
import cn.stylefeng.guns.modular.system.model.params.PurchaseOrdersParam;
import cn.stylefeng.guns.modular.system.model.params.PutInWarehouseParam;
import cn.stylefeng.guns.modular.system.model.params.paramsDTO.WarhouseDTO;
import cn.stylefeng.guns.modular.system.model.result.resultVO.PutInWarehouseVO;
import cn.stylefeng.guns.modular.system.service.DealerLevelService;
import cn.stylefeng.guns.modular.system.service.InventoryOrderService;
import cn.stylefeng.guns.modular.system.service.PutInWarehouseService;
import cn.stylefeng.guns.modular.system.service.ShiWarehouseService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 入库表控制器
 *
 * @author 小飞
 * @Date 2019-06-26 20:37:14
 */
@Controller
@RequestMapping("/putInWarehouse")
public class PutInWarehouseController extends BaseController {

    private String PREFIX = "/putInWarehouse";

    @Autowired
    private PutInWarehouseService putInWarehouseService;
    @Autowired
    private ShiWarehouseService shiWarehouseService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private InventoryOrderService inventoryOrderService;
    @Autowired
    private  DealerLevelService dealerLevelService;
    @Autowired
    private PutInWarehouseMapper putInWarehouseMapper;

    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @RequestMapping("")
    public String index(Model model) {

        model.addAttribute("list",putInWarehouseService.list());
        return PREFIX + "/putInWarehouse.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @RequestMapping("/add")
    public String add(Model model) {

        //经销商登录名
        ShiroUser shiroUser =  ShiroKit.getUserNotNull();
        model.addAttribute("username",shiroUser.getName());

        //部门
        String deptName = deptService.getById(shiroUser.getDeptId()).getSimpleName();
        model.addAttribute("deptName",deptName);

        //经销商名称
       /* String dealerName = dealerLevelService.getOne(new QueryWrapper<DealerLevel>()
                .eq("sys_user_id",shiroUser.getId())).getName();
        model.addAttribute("dealerName",dealerName);
        List<ShiWarehouse> shiWarehouses = shiWarehouseService.list();
        model.addAttribute("warhouses",shiWarehouses);*/
        return PREFIX + "/putInWarehouse_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/putInWarehouse_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(PutInWarehouseParam putInWarehouseParam) {

        ShiroUser shiroUser = ShiroKit.getUserNotNull();
        putInWarehouseParam.setOperator(shiroUser.getName());
        if (ToolUtil.isNotEmpty(shiroUser)) {
            if (ToolUtil.isNotEmpty(deptService.getById(shiroUser.getDeptId()))) {
                putInWarehouseParam.setOperatorDept(deptService.getById(shiroUser.getDeptId()).getFullName());
            }
        }
        putInWarehouseParam.setCreateTime(new Date());
        putInWarehouseParam.setCode(1);
        this.putInWarehouseService.add(putInWarehouseParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(PutInWarehouseParam putInWarehouseParam) {
        this.putInWarehouseService.update(putInWarehouseParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(PutInWarehouseParam putInWarehouseParam) {
        this.putInWarehouseService.delete(putInWarehouseParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(PutInWarehouseParam putInWarehouseParam) {
        PutInWarehouse detail = this.putInWarehouseService.getById(putInWarehouseParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(PutInWarehouseParam putInWarehouseParam) {
        return this.putInWarehouseService.findPageBySpec(putInWarehouseParam);
    }

    /**
     * 入库接口
     * @param id
     * @return
     */
    @RequestMapping("/putInWarhouse")
    @ResponseBody
    public  ResponseData putInWarhouse(@RequestParam("id") Long id){

        //查出当前入库信息
        PutInWarehouse putInWarehouse = putInWarehouseService.getById(id);
        InventoryOrder inventoryOrder = new InventoryOrder();
            if (ToolUtil.isNotEmpty(putInWarehouse)){
                inventoryOrder.setAmount((long)1);
                inventoryOrder.setProductOrderId(putInWarehouse.getProductOrderId());
                inventoryOrder.setSerialNum(putInWarehouse.getSerialNum());
                inventoryOrder.setBatchNumber(putInWarehouse.getBatchNumber());
                inventoryOrder.setWarhouseId(putInWarehouse.getWarehouseId());
                inventoryOrder.setProductOrderId(putInWarehouse.getProductOrderId());
                inventoryOrder.setAmount(putInWarehouse.getAmount());
                inventoryOrderService.save(inventoryOrder);

                //更改库库存信息的状态
                putInWarehouse.setCode(2);
                putInWarehouseService.getBaseMapper().updateById(putInWarehouse);
                return  ResponseData.success();
            }else {
                return  ResponseData.error(202,"暂无当前入库信息！");
            }
        }


        /**
         * 查询数据
         * @return
         */
        @ResponseBody
        @RequestMapping("/treeList")
        public TreeBack list(){

             List<PutInWarehouseVO> lists = putInWarehouseMapper.getList();
             return  new TreeBack(0,"ok",lists);

        }


    /**
     * 批量插入接口
     * @param putInWarehouseParam
     * @return
     */
        @RequestMapping("/submit")
        @ResponseBody
        public  ResponseData submit(PutInWarehouseParam putInWarehouseParam){

          List<WarhouseDTO> warhouseDTOS = JSON.parseArray(putInWarehouseParam.getPurchaseDetail(),WarhouseDTO.class);
          for (WarhouseDTO warhouseDTO : warhouseDTOS){

              //批量插入到入库单中
             PutInWarehouse putInWarehouse = new PutInWarehouse();
             putInWarehouse.setCode(1);
             putInWarehouse.setBatchNumber(warhouseDTO.getBatchNumber());
             putInWarehouse.setSerialNum(warhouseDTO.getSerialNum());
             putInWarehouse.setProductOrderId(warhouseDTO.getProductId());
             putInWarehouse.setWarehouseId(warhouseDTO.getWarhouseId());
             putInWarehouse.setCreateTime(new Date());
             putInWarehouse.setOperator(ShiroKit.getUserNotNull().getName());
             putInWarehouse.setOperatorDept(deptService.getById(ShiroKit.getUserNotNull().getDeptId()).getSimpleName());
              //求出数量
              String[] serialNum = warhouseDTO.getSerialNum().split(",");
              putInWarehouse.setAmount((long)serialNum.length);
              putInWarehouseService.getBaseMapper().insert(putInWarehouse);
          }
          return  ResponseData.success();
        }
}


